{% block files %}
<ul>
  {%- set root_section = get_section(path="_index.md") -%}
  {% set_global tabindex = -1 %}

  {%- for p in root_section.pages %}
  {%- set_global tabindex = tabindex - 1 -%}
  <li class="file">
    <span><a {% if page and p == page -%}class="selected"{%- endif -%}
      href='{{ p.permalink | safe }}'
      tabindex="{{ tabindex }}">  {{ p.title }}</a></span>
  </li>
  {%- endfor -%}

  {% if root_section.subsections %}
    {{- self::fs(root_section=root_section, tabindex=tabindex) -}}
  {% endif %}
</ul>
{% endblock %}


{%- macro fs(root_section, tabindex, folder=False) -%}

{% if root_section.subsections %}

{%- for s in root_section.subsections -%}
  {%- set s = get_section(path=s) -%}

  {%- if folder -%}
  <li class="subfolder">
  {%- else -%}
  <li class="folder">
  {%- endif -%}
    <span>{% if folder %}└──{%- endif %}
      {% set_global tabindex = tabindex - 1 %}
      <a {% if section and s == section -%}class="selected"{%- endif -%}
        href='{{s.permalink | safe }}'
        tabindex="{{ tabindex }}">  {{ s.title }}
      </a>
    </span>

    <ul>
      {% for p in s.pages -%}
      <li class="file">
        <span>└──
          {%- set_global tabindex = tabindex - 1 %}
          <a {% if page and p == page -%}class="selected"{%- endif -%}
            href='{{ p.permalink | safe }}'
            tabindex="{{ tabindex }}">  {{ p.title }}</a>
        </span>
      </li>
      {%- endfor -%}

      {% set subsections = s.subsections %}
        {% if subsections %}
        {% for s in subsections -%}
          {% set s = get_section(path=s) -%}
          {% set fs =  self::fs(root_section=s, tabindex=tabindex, folder=True) %}
          {% set_global tabindex = fs | split(pat="tabindex") | last | split(pat='"') | nth(n=1) | int %}
          {{ fs | safe }}
        {% endfor -%}
      {% endif %}
    </ul>
  </li>
{%- endfor -%}

{% else %}
<li>
<span>└──
  {% set_global tabindex = tabindex - 1 %}
  <a {% if section and root_section == section -%}class="selected"{%- endif -%}
    href='{{root_section.permalink | safe }}'
    tabindex="{{ tabindex }}">  {{ root_section.title }}
  </a>
</span>


  <ul>
  {% for p in root_section.pages -%}
  <li class="file">
    <span>└──
      {%- set_global tabindex = tabindex - 1 %}
      <a {% if page and p == page -%}class="selected"{%- endif -%}
        href='{{ p.permalink | safe }}'
        tabindex="{{ tabindex }}">  {{ p.title }}</a>
    </span>
  </li>
  {%- endfor -%}
  </ul>
</li>
{% endif %}

{%- endmacro fs -%}
